package com.sangcomz.fishbun.ui.detail.mvp;

import com.sangcomz.fishbun.ui.detail.DetailImageContract;
import com.sangcomz.fishbun.ui.detail.model.DetailImageRepository;
import com.sangcomz.fishbun.ui.detail.model.DetailImageViewData;
import ohos.utils.net.Uri;

import java.util.List;

/**
 * Created by sangcomz on 11/06/2017.
 */
public class DetailImagePresenter implements DetailImageContract.Presenter {

    private DetailImageContract.View detailView;
    private DetailImageRepository detailImageRepository;

    public DetailImagePresenter(DetailImageContract.View detailView, DetailImageRepository detailImageRepository) {
        this.detailView = detailView;
        this.detailImageRepository = detailImageRepository;
    }

    @Override
    public void changeButtonStatus(int position) {
        Uri pickerImage = detailImageRepository.getPickerImage(position);
        changeButtonStatusInternal(pickerImage);
    }

    @Override
    public void handleOnCreate(int initPosition) {
        getDesignViewData();
        initViewPagerAdapter();
        initPagerPosition(initPosition);
    }

    @Override
    public void onCountClick(int position) {
        Uri imageUri = detailImageRepository.getPickerImage(position);
        if (imageUri == null) return;
        boolean isSelected = detailImageRepository.isSelected(imageUri);

        if (isSelected) {
            detailImageRepository.unselectImage(imageUri);
        } else {
            if (detailImageRepository.isFullSelected()) {
                detailView.showSnackbar(detailImageRepository.getMessageLimitReached());
            } else {
                detailImageRepository.selectImage(imageUri);
                if (detailImageRepository.checkForFinish()) {
                    detailView.finishActivity();
                }
            }
        }

        changeButtonStatusInternal(imageUri);
    }


    private void getDesignViewData() {
        DetailImageViewData detailImageViewData = detailImageRepository.getDetailPickerViewData();
        detailView.setToolBar(detailImageViewData);
        detailView.setCountButton(detailImageViewData);
        detailView.setBackButton();
    }

    private void initPagerPosition(int position) {
        List<Uri> pickerImages = detailImageRepository.getPickerImages();

        if (!pickerImages.isEmpty()) {
            changeButtonStatus(position);
            detailView.showImages(position, pickerImages);
        } else {
            detailView.finishAndShowErrorToast();
        }
    }

    private void initViewPagerAdapter() {
        detailView.initViewPagerAdapter(detailImageRepository.getImageAdapter());
    }

    private void changeButtonStatusInternal(Uri imageUri) {
        int imageIndex = detailImageRepository.getImageIndex(imageUri);
        if (imageIndex < 0) {
            detailView.unselectImage();
        } else {
            if (detailImageRepository.getMaxCount() == 1) {
                detailView.updateRadioButtonWithDrawable();
            } else {
                detailView.updateRadioButtonWithText(String.valueOf(imageIndex+1));
            }
        }
    }
}